#delimit;
clear;
set more off;

global temp /Sastemp;
global path ~;
set mem 5000m;

/*================================================
 Program: sexregs_revised.do
 Author:  Avi Ebenstein
 Created: August 2007
 Purpose: OLS regs by sex
=================================================*/

global controls "yrsed farmer urban lnairpollution i.region i.cla3";

use ~/pollution/datafiles/dsp_basins.dta, replace;
global causelist "
maledr9
maledr_090
maledr_091
maledr_095
maledr_099
femaledr9
femaledr_090
femaledr_091
femaledr_095
femaledr_099
";

global mylist "overall_q";

for any manufacturing2 paper coal electric: replace share_X=0 if share_X==.;
gen deathrate_099=deathrate9-deathrate_090-deathrate_091-deathrate_095;
gen maledr_099=maledr9-maledr_090-maledr_091-maledr_095;
gen femaledr_099=femaledr9-femaledr_090-femaledr_091-femaledr_095;

for any 090 091 095 099: gen lndeathrate_X=ln(deathrate_X);
for any 090 091 095 099: replace lndeathrate_X=0 if lndeathrate_X==.;

*capture drop urban;
*gen urbanshare=share_tap_water1>.50;
gen urbanshare=1;

forvalues k=1/1{; 
foreach j of global causelist{;
                 gen lndeathrate_`j'_`k'=ln(`j');
foreach i of global mylist{;
                           preserve;
                           replace `i'=. if `i'==0;
                           xi: reg lndeathrate_`j'_`k' `i' $controls [w=totalpop] if urbanshare==`k',cluster(provgb) robust;
                           gen beta=_b[`i'];
                           gen se=_se[`i'];
                           gen urbtype=`k';
                           capture drop avg;
                           egen avg=mean(`j');
                           gen t=beta/se;
                           gen str cause="`j'";
                           gen str chemical="`i'";
                           keep cause chemical beta se t avg urbtype;
                           keep in 1;
                           save ~/pollution/datafiles/run_`i'_`j'_`k', replace;
                           restore;
                         };
               };
              };

clear;
set obs 1;
                 gen a=1;
forvalues k=1/1{;                 
foreach j of global causelist{;
foreach i of global mylist{;
                           append using ~/pollution/datafiles/run_`i'_`j'_`k';
                         };
               };
};  
replace chemical="a_a_overall_q" if chemical=="overall_q";
replace cause="a_maledr_080" if cause=="maledr9";
replace cause="a_maledr_090" if cause=="maledr_090";
replace cause="a_maledr_091" if cause=="maledr_091";
replace cause="a_maledr_095" if cause=="maledr_095";
replace cause="a_maledr_099" if cause=="maledr_099";
replace cause="b_femaledr_080" if cause=="femaledr9";
replace cause="b_femaledr_090" if cause=="femaledr_090";
replace cause="b_femaledr_091" if cause=="femaledr_091";
replace cause="b_femaledr_095" if cause=="femaledr_095";
replace cause="b_femaledr_099" if cause=="femaledr_099";

capture log close;
gen dummy=1;
set linesize 250;
log using ~/pollution/logfiles/regs_icd9_3.log, replace;
table cause urbtype, c(mean beta mean se) f(%9.3fc) cellwidth(16);
capture log close;

!rm ~/pollution/datafiles/run_*.dta, force;                 
